// AWTRowLayout.cpp: implementation of the AWTRowLayout class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWTRowLayout.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTRowLayout, AWTLayout)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AWTRowLayout::AWTRowLayout()
{
	m_gap = 5;
}

AWTRowLayout::AWTRowLayout(int gap)
{
	m_gap = gap;
}

AWTRowLayout::~AWTRowLayout()
{

}

void AWTRowLayout::addLayoutComponent(CString* name, AWTComponent* comp)
{
}

void AWTRowLayout::removeLayoutComponent(AWTComponent* comp)
{
}

CSize AWTRowLayout::preferredLayoutSize(AWTContainer* target)
{
	CSize dim(0,0);
	AWTInsets insets = target->getInsets();
	int ncomponents = target->getComponentCount();
	AWTComponent* comp;
	CSize d;

	for (int i = 0 ; i < ncomponents ; i++) 
	{
		comp = target->getComponent(i);
		if(comp->isVisible()) 
		{
			d = comp->getPreferredSize();
			dim.cx += d.cx;
			dim.cy  = max(d.cy, dim.cy);
			if (i > 0) 
				dim.cx += m_gap;
		}
	}
	dim.cx += insets.left + insets.right;
	dim.cy += insets.top + insets.bottom;
	return dim;
}

CSize AWTRowLayout::minimumLayoutSize(AWTContainer* target)
{
	CSize dim(0,0);
	AWTInsets insets = target->getInsets();
	int ncomponents = target->getComponentCount();
	AWTComponent* comp;
	CSize d;

	for (int i = 0 ; i < ncomponents ; i++) 
	{
		comp = target->getComponent(i);
		if(comp->isVisible()) 
		{
			d = comp->getPreferredSize();
			dim.cx += d.cx;
			dim.cy  = max(d.cy, dim.cy);
			if (i > 0) 
				dim.cx += m_gap;
		}
	}
	dim.cx += insets.left + insets.right;
	dim.cy += insets.top + insets.bottom;
	return dim;
}

void AWTRowLayout::layoutContainer(AWTContainer* target)
{
	AWTInsets insets = target->getInsets();
	int ncomponents = target->getComponentCount();
	int top = 0;
	int left = insets.left;
	CSize tps = target->getPreferredSize();
	CSize targetSize = target->getPreferredSize();
//	CSize targetSize = target->getSize();
	AWTComponent* comp;
	CSize ps;

	left = left + (targetSize.cx/2) - (tps.cx/2);

	for (int i = 0 ; i < ncomponents ; i++) 
	{
		comp = target->getComponent(i);
		if(comp->isVisible()) 
		{
			ps  = comp->getPreferredSize();
			top = (targetSize.cy/2) - (ps.cy/2);
			
			if (top + ps.cy > target->getHeight())
			{
				ps.cy = (ps.cy - ((top + ps.cy) - target->getHeight()));
			}

			comp->setBounds(left,top,ps.cx,ps.cy);
			left += ps.cx + m_gap;
		}
	}
}
